package it.polimi.travelDream.ejb.servlet;

import it.polimi.travelDream.ejb.interfaces.ExcursionMgrRemote;







import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class EditExcursionServlet
 */
@WebServlet("/EditExcursionServlet")
public class EditExcursionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
	@EJB
	ExcursionMgrRemote hmr;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditExcursionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
Map<String, String[]> mappa = request.getParameterMap();
format.setLenient(false);
/*Empty Fields Check*/
if (mappa.get("name")[0].isEmpty() ||mappa.get("descr")[0].isEmpty() || mappa.get("cost")[0].isEmpty() || mappa.get("duration")[0].isEmpty() || mappa.get("avalpax")[0].isEmpty() || mappa.get("avday")[0].isEmpty())
 {
	request.setAttribute("message","Empty fields");
	request.getRequestDispatcher("/BPEditExcursion.jsp").forward(request, response); 
 }else
 {
	 try { Integer.parseInt(mappa.get("totalCost")[0]);
	 Integer.parseInt(mappa.get("availabilityPax")[0]);
		 format.parse(mappa.get("avday")[0]);
		 /*Excursion Insertion*/

		String id=mappa.get("id")[0];
		String name=mappa.get("name")[0];
		String descr=mappa.get("descr")[0];
		String duration=mappa.get("duration")[0];
		String avday=mappa.get("avday")[0];
		String avalpax=mappa.get("avalpax")[0];
		String cost=mappa.get("cost")[0];
		hmr.modifyExcursion(id, name, descr, duration, avday, avalpax, cost);
		request.getRequestDispatcher("/BPEditExcursion.jsp").forward(request, response);
		
	 } catch (ParseException e) {
		 
		 request.setAttribute("message","Date must be [dd/mm/yyyy]");
			request.getRequestDispatcher("/BPEditExcursion.jsp").forward(request, response); 
		 
	 }
		
	}

}}